package com.example.resource_station_backend.controller;


import com.example.resource_station_backend.common.BaseResponse;
import com.example.resource_station_backend.common.ResultUtils;
import com.example.resource_station_backend.dao.ArticlesRepository;
import com.example.resource_station_backend.dao.CategoryRepository;
import com.example.resource_station_backend.exception.ErrorCode;
import com.example.resource_station_backend.exception.ThrowUtils;
import com.example.resource_station_backend.model.dto.articles.ArticlesAddRequest;
import com.example.resource_station_backend.model.dto.articles.ArticlesQueryRequest;
import com.example.resource_station_backend.model.entity.Articles;
import com.example.resource_station_backend.model.vo.ArticlesVO;
import com.example.resource_station_backend.service.ArticlesService;
import com.example.resource_station_backend.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;


/**
 * @Author xinhao
 * @Description
 * @CreateTime 2025-02-24 17:38:16
 */
@RestController
@RequestMapping("/articles")
public class ArticleController {

    @Autowired
    ArticlesRepository articlesRepository;

    @Autowired
    ArticlesService articlesService;

    @Autowired
    CategoryService categoryService;

    /**
     * 返回文章列表(脱敏)
     */
    @GetMapping()
    public BaseResponse<List<ArticlesQueryRequest>> getArticleList() {
        // 获取分页数据
        List<ArticlesQueryRequest> articlesQueryRequestList = articlesRepository.findArticlesWithoutContent();
        ThrowUtils.throwIf(articlesQueryRequestList.isEmpty(), ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(articlesQueryRequestList);
    }

    /**
     * 返回分类后的文章列表(脱敏)
     */
    @GetMapping("/{categoryName}/list")
    public BaseResponse<List<ArticlesQueryRequest>> getArticleListByCategory(@PathVariable String categoryName) {
        Long categoryId = categoryService.getCategoryIdByName(categoryName);
        // 根据分类id获取文章列表
        List<ArticlesQueryRequest> articlesQueryRequestList = articlesRepository.findByCategoryIdWithoutContent(categoryId);
        ThrowUtils.throwIf(articlesQueryRequestList.isEmpty(), ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(articlesQueryRequestList);
    }

    /**
     * 文章详细，包含文章内容
     */
    @GetMapping("/{articlesId}")
    public BaseResponse<ArticlesVO> getArticleDetails(@PathVariable Long articlesId) {
        Optional<Articles> articlesById = articlesRepository.findById(articlesId);
        ThrowUtils.throwIf(articlesById.isEmpty(), ErrorCode.PARAMS_ERROR);
        Articles articles = articlesById.get();
        ArticlesVO articlesVO = articlesService.objToVo(articles);
        return ResultUtils.success(articlesVO);
    }

    /**
     * 根据分类名称返回文章详细
     */
    @GetMapping("/{category}/{articlesId}")
    public BaseResponse<ArticlesVO> getArticleDetailsByCategory(@PathVariable String category, @PathVariable Long articlesId) {
        Optional<Articles> articlesById = articlesRepository.findById(articlesId);
        ThrowUtils.throwIf(articlesById.isEmpty(), ErrorCode.PARAMS_ERROR);
        Articles articles = articlesById.get();
        ArticlesVO articlesVO = articlesService.objToVo(articles);
        ThrowUtils.throwIf(!category.equals(articlesVO.getCategory()), ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(articlesVO);
    }

    /**
     * 手动添加文章
     */
    @PostMapping("/add")
    public BaseResponse<Long> addArticle(@RequestBody ArticlesAddRequest articlesAddRequest) {
        ThrowUtils.throwIf(articlesAddRequest == null, ErrorCode.PARAMS_ERROR);

        Long result = articlesService.addArticle(articlesAddRequest);
        return ResultUtils.success(result);
    }

}
